package com.cmcc.cmii.cmiiservicemanagement.nacos;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.cmcc.cmii.cmiiservicemanagement.config.ServiceDiscoveryClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @author Thanks
 */
@Configuration
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class NacosServiceDiscoveryClient implements ServiceDiscoveryClient {
    private final NacosServiceDiscoveryProperties wiiNacosDiscoveryProperties;

    @Override
    public void registerInstance(String applicationName, String ip, Integer port) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance()
                    .registerInstance(applicationName, ip, port);
        } catch (NacosException e) {
            log.warn("nacos exception happens", e);
        }
    }

    @Override
    public void deregisterInstance(String applicationName, String ip, Integer port) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance()
                    .deregisterInstance(applicationName, ip, port);
        } catch (NacosException e) {
            log.warn("nacos exception happens", e);
        }
    }



    @Override
    public void registerInstance(String serviceName, String groupName, String ip, int port) {
        try{
            this.wiiNacosDiscoveryProperties.namingServiceInstance().registerInstance(serviceName,groupName,ip,port);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }

    }

    @Override
    public void registerInstance(String serviceName, String ip, int port, String clusterName) {
        try{
            this.wiiNacosDiscoveryProperties.namingServiceInstance().registerInstance(serviceName,ip,port,clusterName);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }

    @Override
    public void registerInstance(String serviceName, String groupName, String ip, int port, String clusterName) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance().registerInstance(serviceName,groupName,ip,port,clusterName);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }


    @Override
    public void deregisterInstance(String serviceName, String groupName, String ip, int port) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance().deregisterInstance(serviceName,groupName,ip,port);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }

    @Override
    public void deregisterInstance(String serviceName, String ip, int port, String clusterName) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance().deregisterInstance(serviceName,ip,port,clusterName);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }

    @Override
    public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance().deregisterInstance(serviceName,groupName,ip,port,clusterName);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }

    @Override
    public void registerInstance(String serviceName, Instance instance) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance().registerInstance(serviceName,instance);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }

    @Override
    public void registerInstance(String serviceName, String groupName, Instance instance) {
        try {
            this.wiiNacosDiscoveryProperties.namingServiceInstance().deregisterInstance(serviceName,groupName,instance);
        }catch (Exception e){
            log.warn("nacos exception happens", e);
        }
    }
}
